<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use GatewayClient\Gateway;

class ChatSocket extends Controller
{
    //处理推送业务

    /**
     * 初始化业务
     */
    public function __construct()
    {
        /**
         *  GatewayClient和Register服务都在一台服务器上，ip填写127.0.0.1及即可，无需其它设置。
         *  告知 GatewayClient 与哪个 GatewayWorker （集群）通讯
         */
        Gateway::$registerAddress = '127.0.0.1:1238'; 


    }


    /**
     * web客户端
     * @param string $uid
     * @param string $to_uid
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function client()
    {
        return view('client');
    }

    /**
     * 绑定uid
     * @return mixed
     */
    public function bind()
    {
        // 假设用户已经登录，用户uid和群组id在session中
        $uid = request('uid');
        $client_id = request('client_id');

        $res = request()->all();
        $res['type'] = 'bind';
        $res['time'] = date('H:i:s');
        // client_id与uid绑定
        Gateway::bindUid($client_id, $uid);
        Gateway::sendToUid($uid, json_encode($res));
        return response()->json($res);
    }

    /**
     * 发送消息
     * @return mixed
     */
    public function send()
    {
        $uid = request('uid');
        $to_uid = request('to_uid');
        $res = request()->all();
        $res['type'] = 'send';
        $res['time'] = date('H:i:s');
        // 向任意uid的网站页面发送数据
        Gateway::sendToUid($uid, json_encode($res));
        Gateway::sendToUid($to_uid, json_encode($res));
        return response()->json($res);
    }
    

    /**
     * 处理一对一私聊
     */
    public function PrivateChat(Request $request)
    {


    }   

    /**
     * 处理群聊
     */
    public function GroupChat()
    {


    }


    /**
     * 处理广播所有人推送
     */
    public function Broadcast()
    {


    }
     
}
